package program2;
import java.net.*;
import java.util.Properties;
import java.util.concurrent.*;
import java.io.*;
import java.time.LocalDateTime;
public class server {//服务端
	public static void main(String[] args) throws IOException
	{
		ThreadPoolExecutor pool = new ThreadPoolExecutor(//创建线程池对象
				3,//核心线程数量
				16,//线程池总大小
				60,//空闲时间
				TimeUnit.SECONDS,//空闲时间（单位）
				new ArrayBlockingQueue<>(2),//队列
				Executors.defaultThreadFactory(),//线程工厂
				new ThreadPoolExecutor.AbortPolicy());//阻塞队列
			int port = readPort();			
			ServerSocket ss = new ServerSocket(port);	
		while(true)	
			{
				Socket socket = ss.accept();				
				InetAddress address = socket.getInetAddress();
				System.out.println("IP: "+address+"连接成功！");
				getID(address);
				pool.submit(new MyRunnable(socket));
			}
			
	}

	public static int readPort() //监听端口
	{		
		Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream("./hotel message/server.config.properties")) {
            properties.load(fis);
            String portStr = properties.getProperty("server.port");
            return Integer.parseInt(portStr);
        } catch (IOException e) {
            e.printStackTrace();
            return -1; // 默认返回一个无效端口号表示读取失败
        }
	}
		
	public static void getID(InetAddress address) throws IOException
	{
		FileOutputStream fos = new FileOutputStream("./hotel message/ip.txt",true);//创建输入流
		LocalDateTime now = LocalDateTime.now(); // 获取现在的时间  
		fos.write(now.toString().getBytes());
		fos.write(":  IP".getBytes());
		fos.write(address.getHostAddress().getBytes());
		fos.write("连接到客户端\n".getBytes());
		

	}
}

